﻿' Design Decision
' A MVC pattern was not used here as the additional cost of adding to the 
' Global.asax and Web.Config is not worth the hassle when this application must
' be loaded on a pre existing site.
' Future seperate administration functions may be different

Partial Public Class AdvertisementUpload
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            OnViewInitialized()
        End If
        OnViewLoaded()
    End Sub

    ''' <summary>
    ''' This is called every time the page loads
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub OnViewLoaded()

    End Sub

    ''' <summary>
    ''' This is called on the first page load
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub OnViewInitialized()
        Dim manager As New DotNetCore.BannerManagement.BLL.AdvertiserManager

        ddlAdvertisers.DataSource = manager.GetAllAdvertisers()
        ddlAdvertisers.DataBind()

    End Sub

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
        Dim intLength As Integer

        Dim arrContent As Byte()

        If fuBanner.PostedFile Is Nothing Then

            Exit Sub

        Else

            Dim fileName As String = fuBanner.PostedFile.FileName '.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("/") + 1)

            Dim ext As String = fileName.Substring(fileName.LastIndexOf("."))

            ext = ext.ToLower.Replace(".", "")


        End If



        intLength = Convert.ToInt32(fuBanner.PostedFile.InputStream.Length)

        ReDim arrContent(intLength)



        fuBanner.PostedFile.InputStream.Read(arrContent, 0, intLength)

        Dim UploadedImage As System.Drawing.Image = System.Drawing.Image.FromStream(fuBanner.PostedFile.InputStream)


        Dim manager As New DotNetCore.BannerManagement.BLL.ImageManager

        Dim img As New DotNetCore.BannerManagement.Domain.dnc_Image

        img.Image = arrContent
        img.ContentType = fuBanner.PostedFile.ContentType
        img.Name = txtName.Text
        img.Width = UploadedImage.Width
        img.Height = UploadedImage.Height


        manager.InsertImage(img)

        Dim adManager As New DotNetCore.BannerManagement.BLL.AdvertisementManager

        Dim advertisement As New Domain.dnc_Advertisement
        advertisement.Active = True
        advertisement.AdvertiserID = ddlAdvertisers.SelectedIndex
        advertisement.AlternateText = txtAltText.Text
        advertisement.EndDate = Date.ParseExact(txtEndDate.Text, "dd/MM/yyyy", Nothing)
        advertisement.startDate = Date.ParseExact(txtStartDate.Text, "dd/MM/yyyy", Nothing)
        advertisement.ImageID = img.ID
        advertisement.Name = txtName.Text
        advertisement.URL = txtURL.Text

        adManager.InsertAdvertisement(advertisement)

    End Sub


End Class